import store from '../vuex/store';
import {isAndroid, isiOS} from './device';
import constant from './constant';
import mintUI from './mintUi';
import api from '@/common/api/api';
/**
 * @module common/native
 * @author ashen
 * @description  与安卓交互相关方法
 */
var actions = '';
if (isAndroid) {
  actions = window.ljc;
} else if (isiOS) {
  actions = window;
}
var nativeAction = {};
window.setRegId = function (data) {
  const regId = JSON.parse(data).regId;
  const osType = JSON.parse(data).device;
  constant.phoneInfo.regId = regId;
  constant.phoneInfo.osType = osType;
  if (constant.phoneInfo.username) {
    api.setPhoneType(constant.phoneInfo);
  }
};
if (actions) {
  nativeAction = {
    /**
     * @function
     * @param  {Object} info 传入参数
     * @description 改变用户信息的时候需要更新native
     */
    setInfo (info) {
      store.dispatch('setUserInfo', info);
      const url = 'api/sys/user/updatePhoto';
      actions.setLogin(JSON.stringify(window.sessionStorage), window.sessionStorage.userToken, constant.ajaxUrl() + url);
    },
    /**
     * @function
     * @description 退出登录需要更新native清除用户缓存信息
     */
    removeLogin () {
      actions.removeLogin();
    },
    /**
     * @function
     * @description 获取native设备号
     */
    getPushRegId () {
      let data = actions.getPushRegId();
      return {
        regId: JSON.parse(data).regId,
        osType: JSON.parse(data).device
      };
    },
    /**
     * @function
     * @description 从native里获取userToken，userInfo，store
     */
    getLogin () {
      const info = JSON.parse(actions.getLogin());
      store.dispatch('setToken', info.userToken);
      store.dispatch('setUserInfo', JSON.parse(info.userInfo));
      if (info.store) {
        store.dispatch('setStore', JSON.parse(info.store));
      }
    },
    iosgetLogin () {
      const info = JSON.parse(actions.getLogin());
      // alert(info.userInfo);
      store.dispatch('setToken', info.userToken);
      store.dispatch('setUserInfo', info.userInfo);
      if (info.store) {
        store.dispatch('setStore', info.store);
      }
    },
    /**
     * @function
     * @description 调用系统通讯录,native点击邀请调用window.getContacts方法
     */
    pickContacts () {
      return actions.pickContacts();
    },
    /**
     * @function
     * @description 头像上传，两种方式，0为选取系统相册，1为系统拍照
     */
    changeImages (type, that) {
      window.getPhoto = (photo) => {
        that.userInfo.photo = photo;
        let userInfo = JSON.parse(JSON.stringify(that.userInfo));
        this.setInfo(userInfo);
      };
      actions.takePhoto(type);
    },
    /**
     * @function
     * @description 二维码扫描
     */
    scan () {
      actions.scan();
    }
  };
} else {
  nativeAction = {
    /**
     * @function
     * @param  {Object} info 传入参数
     * @description 改变用户信息的时候需要更新native
     */
    setInfo (info) {
      store.dispatch('setUserInfo', info);
    },
    /**
     * @function
     * @description 退出登录需要更新native清除用户缓存信息
     */
    removeLogin () {},
    /**
     * @function
     * @description 获取native设备号
     */
    getPushRegId () {
      return null;
    },
    /**
     * @function
     * @description 从native里获取userToken，userInfo，store
     */
    getLogin () { },
    /**
     * @function
     * @description 调用系统通讯录,native点击邀请调用window.getContacts方法
     */
    pickContacts () {
      mintUI.openToast('请下载官方APP龙决策,在APP中体验此功能');
    },
    /**
     * @function
     * @description 头像上传，两种方式，0为选取系统相册，1为系统拍照
     */
    changeImages () {
      mintUI.openToast('请下载官方APP龙决策,在APP中体验此功能');
    },
    /**
     * @function
     * @description 二维码扫描
     */
    scan () {
      mintUI.openToast('请下载官方APP龙决策,在APP中体验此功能');
    }
  };
}
//ios端使用
window.iosnative = nativeAction;

export default nativeAction;
